package com.bianguo.android.functiondemo.view.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.opengl.GLSurfaceView;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.bianguo.android.functiondemo.R;
import com.bianguo.android.functiondemo.easyAR.GLView;
import com.bianguo.android.functiondemo.utils.ContactUtil;
import com.bianguo.android.functiondemo.utils.PermissionHelper;

import butterknife.BindView;
import cn.easyar.Engine;

public class EasyARActivity extends BaseActivity {

    @BindView(R.id.easy_ar_frame)
    FrameLayout frameLayout;

    private GLView mGLView;

    @Override
    public int layoutId() {
        return R.layout.activity_easy_ar;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        Engine.initialize(mContext, ContactUtil.EASY_AR_LICENSE_KEY);
        boolean isPermission = PermissionHelper.getInstance(mContext).isPermission(mContext, new String[]{Manifest.permission.CAMERA}, PermissionHelper.PERMISSION_CAMERA);
        if (isPermission) {
            mGLView = new GLView(this);
            frameLayout.addView(mGLView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mGLView != null) {
            mGLView.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mGLView != null) {
            mGLView.onPause();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case PermissionHelper.PERMISSION_CAMERA:
                if (PackageManager.PERMISSION_GRANTED == grantResults[0]) {
                    mGLView = new GLView(this);
                    frameLayout.addView(mGLView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                } else {

                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
                break;
        }
    }
}
